By the end of today, you should:
[] and $To understand […] R, two slogans are helpful:
Everything that exists is an object
Everything that happens is a function call
– John Chambers
A function:
function_name(argument1, argument2, ...)
You can write your own functions.
Functions can be:
👉 Collections of useful, published functions form packages. We will expore some next week.
Packages are what make R (and Python) powerful ecosystems.
Without the pipe, nested calls become hard to read:
With the pipe |>, read left to right – “and then”:
The pipe says: take this, and then do that.
| Type | Description | Example |
|---|---|---|
double |
Decimal numbers | 3.14, 54300.5 |
integer |
Whole numbers | 42, 2026 |
character |
Text / strings | "Germany", "hello" |
logical |
True or false | TRUE, FALSE |
factor |
Categories | "low", "medium", "high" |
Date |
Calendar dates | 2026-02-23 |
[1] "numeric"
[1] "integer"
[1] 7
[1] "GERMANY"
[1] "Germany is in the EU"
Numbers stored as characters are not numbers – R won’t do math with them:
[1] TRUE
[1] TRUE
[1] TRUE
[1] FALSE
Logicals are the backbone of filtering – you’ll use them constantly when subsetting data.
| Operator | Meaning | Example |
|---|---|---|
< |
Less than | 3 < 5 |
<= |
Less than or equal | 3 <= 3 |
> |
Greater than | 5 > 3 |
>= |
Greater than or equal | 5 >= 5 |
== |
Equal to | 3 == 3 |
!= |
Not equal to | 3 != 5 |
& |
AND (vectorised) | (3 > 1) & (5 > 2) |
\| |
OR (vectorised) | (3 > 5) \| (5 > 2) |
! |
NOT | !(3 > 5) |
%in% |
Is element contained in vector | "Germany" %in% c("France", "Germany") |
Factors – categorical variables with fixed levels
You’ll encounter these often in real data. The key insight: R treats them differently from plain text so it can do useful things like ordering, arithmetic, and plotting.
| Structure | Dimensions | Types allowed |
|---|---|---|
| Vector | 1D | One |
| Matrix | 2D | One |
| Array | nD | One |
| Data frame | 2D | Multiple |
| List | Any | Multiple |
[1] 5
[1] "character"
[1] "logical"
A vector can only hold one type. If you mix types, R silently converts:
country population eu_member regime
1 Germany 84 TRUE democracy
2 France 68 TRUE democracy
3 Italy 60 TRUE democracy
4 Spain 47 TRUE democracy
5 Turkey 88 FALSE hybrid
[1] 5
[1] 4
country population eu_member regime
1 Germany 84 TRUE democracy
2 France 68 TRUE democracy
3 Italy 60 TRUE democracy
4 Spain 47 TRUE democracy
5 Turkey 88 FALSE hybrid
'data.frame': 5 obs. of 4 variables:
$ country : chr "Germany" "France" "Italy" "Spain" ...
$ population: num 84 68 60 47 88
$ eu_member : logi TRUE TRUE TRUE TRUE FALSE
$ regime : chr "democracy" "democracy" "democracy" "democracy" ...
country population eu_member regime
Length:5 Min. :47.0 Mode :logical Length:5
Class :character 1st Qu.:60.0 FALSE:1 Class :character
Mode :character Median :68.0 TRUE :4 Mode :character
Mean :69.4
3rd Qu.:84.0
Max. :88.0
$name
[1] "Germany"
$population
[1] 8.4e+07
$eu_member
[1] TRUE
$parties
[1] "SPD" "CDU" "Greens" "FDP"
Lists can hold anything – including other lists. They’re less intuitive than data frames, but you’ll encounter them constantly as outputs of statistical models.
Two main tools:
[ ] – index by position or condition$ – index by name (for data frames and lists)[ ][ ] and $ country population eu_member regime
1 Germany 84 TRUE democracy
[1] 84 68 60 47 88
[1] 84
'data.frame': 5 obs. of 4 variables:
$ country : chr "Germany" "France" "Italy" "Spain" ...
$ population: num 84 68 60 47 88
$ eu_member : logi TRUE TRUE TRUE TRUE FALSE
$ regime : chr "democracy" "democracy" "democracy" "democracy" ...
country population eu_member regime
1 Germany 84 TRUE democracy
2 France 68 TRUE democracy
[1] "data.frame"
[1] "numeric"
These are some of the first things you should run whenever you encounter a new dataset.
exercise_week03.qmd in PositronCore tasks (everyone):
[ ] and $Optional depth exercises (fast movers):
table()as.Date() and lubridate.qmd + rendered PDF)The homework follows the same arc as today – you’ll work with a small political dataset to practice types, structures, and subsetting. Read the task descriptions carefully and write comments explaining your reasoning.
QMIR – February 19, 2026 – Week 2
Comments – Code as Communication
View source